package com.bigtotoro.shuiyincamera.fragment;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.bigtotoro.shuiyincamera.Main2Activity;
import com.bigtotoro.shuiyincamera.MainActivity;
import com.bigtotoro.shuiyincamera.MusicCropActivity;
import com.bigtotoro.shuiyincamera.MyArtListActivity;
import com.bigtotoro.shuiyincamera.R;
import com.bigtotoro.shuiyincamera.SettingActivity;
import com.bigtotoro.shuiyincamera.ShowMyArtsActivity;
import com.bigtotoro.shuiyincamera.VideoMainEditActivity;
import com.bigtotoro.shuiyincamera.image.AddWaterMark02Activity;
import com.bigtotoro.shuiyincamera.image.Palette02Activity;
import com.bigtotoro.shuiyincamera.image.RemoveWaterMark2Activity;
import com.bigtotoro.shuiyincamera.model.MyArt;
import com.bigtotoro.shuiyincamera.util.DimenUtils;
import com.bigtotoro.shuiyincamera.util.FileUtils;
import com.bigtotoro.shuiyincamera.util.ModelSingle;
import com.bigtotoro.shuiyincamera.util.VideoItem;
import com.bigtotoro.shuiyincamera.util.VideoUtil;
import com.bigtotoro.shuiyincamera.video.VideoAddWater2Activity;
import com.bigtotoro.shuiyincamera.video.VideoConcatActivity;
import com.bigtotoro.shuiyincamera.video.VideoCropActivity;
import com.bigtotoro.shuiyincamera.video.VideoMusicActivity;
import com.bigtotoro.shuiyincamera.video.trim.VideoTrimmerActivity;
import com.bigtotoro.shuiyincamera.util.MyUtils;
import com.bigtotoro.shuiyincamera.video.VideoRemoveWaterActivity;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.decoration.RecycleViewDivider;
import com.luck.picture.lib.entity.LocalMedia;

import org.litepal.LitePal;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import static android.app.Activity.RESULT_OK;
import static com.bigtotoro.shuiyincamera.editimage.Constacts.EXTRA_OUTPUT;

/**
 * 主页
 */

public class HomeFragment extends Fragment implements View.OnClickListener {
    private View rootview;

    private View layout01;
    private ImageView iv_empty;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootview == null) {
            rootview = inflater.inflate(R.layout.fragment_home, null);
            VideoUtil.checkVideoDir();

            initView();
        }
        ViewGroup parent = (ViewGroup) rootview.getParent();
        if (parent != null) {
            parent.removeView(rootview);
        }
        return rootview;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    private void initView() {

//        findViewById(R.id.btn_setting).setOnClickListener(this);
        iv_empty = rootview.findViewById(R.id.iv_empty);
        iv_empty.setVisibility(View.GONE);
        layout01 = rootview.findViewById(R.id.layout01);
        rootview.findViewById(R.id.btn_start).setOnClickListener(this);  //开始制作
        rootview.findViewById(R.id.btn_main).setOnClickListener(this);  //进入主界面
        rootview.findViewById(R.id.btn_my_art_list).setOnClickListener(this);  //进入作品列表

        rootview.findViewById(R.id.btn_img_add_watermark).setOnClickListener(this);//图片加水印
        rootview.findViewById(R.id.btn_img_remove_watermark).setOnClickListener(this);//图片去水印，还没有这个功能，调用的图片裁剪
//        findViewById(R.id.btn_img_palette).setOnClickListener(this);//图片涂鸦

        rootview.findViewById(R.id.btn_video_add_watermark).setOnClickListener(this);//视频加水印
        rootview.findViewById(R.id.btn_video_remove_water).setOnClickListener(this);//视频去水印
        rootview.findViewById(R.id.btn_video_splice).setOnClickListener(this);//视频拼接
        rootview.findViewById(R.id.btn_video_cut).setOnClickListener(this);//视频剪裁
        rootview.findViewById(R.id.btn_video_edit).setOnClickListener(this);//视频分割
//        findViewById(R.id.btn_video_music).setOnClickListener(this);//背景音乐
//        findViewById(R.id.btn_video_extract_bg).setOnClickListener(this);//分离音频

//        findViewById(R.id.btn_music_edit).setOnClickListener(this);//音频分割


        initRecycleview();
        initData();

    }

    private List<MyArt> mPhotoList = new ArrayList<>();
    private RecyclerView recyclerView;
    private BaseQuickAdapter mAdapter ;
    private void initRecycleview() {
        recyclerView = rootview.findViewById(R.id.recycler);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.addItemDecoration(new RecycleViewDivider(getActivity(),
                LinearLayoutManager.HORIZONTAL,
                DimenUtils.dip2px(getActivity(), 1), Color.parseColor("#eeeeee")));
        mAdapter = new BaseQuickAdapter<MyArt, BaseViewHolder>(
                R.layout.item_grid, mPhotoList){
            @Override
            protected void convert(BaseViewHolder helper, MyArt item) {
                helper.setText(R.id.item_time, "最后修改时间: " + item.getCreate_time())
                        .setText(R.id.item_path, "文件路径: " + item.getPath())
                        .addOnClickListener(R.id.iv_photo);
                ImageView iv_photo = helper.getView(R.id.iv_photo);
                if (item.getType() == 2){
                    iv_photo.setImageResource(R.mipmap.iv_audio);
                }else {
                    if (item.getPath() != null && item.getPath().lastIndexOf(".") > 0) {
                        Glide.with(getActivity()).load(new File(item.getPath())).into(iv_photo);
                    }
                }

                ImageView iv_cover = helper.getView(R.id.iv_cover);
                switch (item.getType()){  //根据类型显示不同的标签
                    case 0: //图片
                        iv_cover.setImageDrawable(getResources().getDrawable(R.mipmap.type_img));
                        break;
                    case 1: //视频
                        iv_cover.setImageDrawable(getResources().getDrawable(R.mipmap.type_video));
                        break;
                    case 2: //音频
                        iv_cover.setImageDrawable(getResources().getDrawable(R.mipmap.type_audio));
                        break;
                }
            }
        };
        recyclerView.setAdapter(mAdapter);
        recyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                //根据当前选中图片还是视频，分别跳转
                MyArt art = mPhotoList.get(position);
                if (art!=null && art.getType()==0){  //图片预览

                    startActivityForResult(new Intent(getActivity(),
                            ShowMyArtsActivity.class)
                            .putExtra(EXTRA_OUTPUT, art.getPath())
                            .putExtra("create_time", art.getTrueCreateTime())
                            .putExtra("position",  position)
                            .putExtra("width",  art.getWidth())
                            .putExtra("height", art.getHeight())
                            .putExtra("mode", "myartlist")
                            .putExtra("type", "0"),REQUEST_CODE_SHOW_IMAGE);

                }else if (art!=null && art.getType()==1){  //视频预览
                    startActivityForResult(new Intent(getActivity(),
                            ShowMyArtsActivity.class)
                            .putExtra(EXTRA_OUTPUT, art.getPath())
                            .putExtra("create_time", art.getTrueCreateTime())
                            .putExtra("position",  position)
                            .putExtra("width",  art.getWidth())
                            .putExtra("height", art.getHeight())
                            .putExtra("mode", "myartlist")
                            .putExtra("type", "1"),REQUEST_CODE_SHOW_VIDEO);
                }else if (art!=null && art.getType()==2){  //音频预览
                    startActivityForResult(new Intent(getActivity(),
                            ShowMyArtsActivity.class)
                            .putExtra(EXTRA_OUTPUT, art.getPath())
                            .putExtra("create_time", art.getTrueCreateTime())
                            .putExtra("position",  position)
                            .putExtra("width",  art.getWidth())
                            .putExtra("height", art.getHeight())
                            .putExtra("mode", "myartlist")
                            .putExtra("type", "2"),REQUEST_CODE_SHOW_AUDIO);

                }

            }
        });
    }

    private static final int REQUEST_CODE_SHOW_IMAGE = 9003;
    private static final int REQUEST_CODE_SHOW_VIDEO = 9004;
    private static final int REQUEST_CODE_SHOW_AUDIO = 9005;

    private void deleteFromLitepal(int position) {
        MyArt art = mPhotoList.get(position);
        LitePal.deleteAll(MyArt.class, "create_time =?", art.getTrueCreateTime());
        initData();
        Toast.makeText(getActivity(), "作品已删除", Toast.LENGTH_SHORT).show();
    }

    private void initData() {
        //应该按时间倒序排列
        List<MyArt> list = LitePal.order("create_time desc").limit(3).find(MyArt.class);
        if (list != null) {
            mPhotoList.clear();
            mPhotoList.addAll(list);
            if (mAdapter!=null){
                mAdapter.notifyDataSetChanged();
            }
            if (list!=null && list.size()>0){
                iv_empty.setVisibility(View.GONE);
//                layout01.setVisibility(View.VISIBLE);
            }else {
                iv_empty.setVisibility(View.VISIBLE);
//                layout01.setVisibility(View.GONE);
            }
        } else {
            iv_empty.setVisibility(View.VISIBLE);
            Toast.makeText(getActivity(), "暂无数据", Toast.LENGTH_SHORT).show();
        }
    }



    public static final int SELECT_IMAGE_ADD_WATER_MARK = 1001;
    public static final int SELECT_IMAGE_CUT = 1002;
    public static final int SELECT_IMAGE_FREE_EDIT = 1003;
    public static final int SELECT_IMAGE_REMOVE_WATER_MARK = 1004;
    public static final int SELECT_MUSIC_CROP = 1005;

    public static final int SELECT_VIDEO_REMOVE_WATER_MARK = 1007;
    public static final int SELECT_VIDEO_ADD_WATER_MARK = 1008;
    public static final int SELECT_VIDEO_EDIT = 1009;
    public static final int SELECT_VIDEO_CROP = 1010;
    public static final int SELECT_VIDEO_MUSIC = 1011;
    public static final int SELECT_VIDEO_EXTRACT_BG = 1013;
    public static final int SELECT_VIDEO_SPLICE = 1012;
    public static final int JUMP_TO_MAIN_EDIT = 1015;
    public static final int REQUEST_PERMISSON_SORAGE = 1;
    private int mRequestCode = -1;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_my_art_list: //进入作品列表
                startActivity(new Intent(getActivity(),MyArtListActivity.class));
                break;
            case R.id.btn_main: //进入主界面
                startActivity(new Intent(getActivity(), Main2Activity.class));
                break;
            case R.id.btn_start: //进入视频编辑总界面
                MyUtils.checkReadAndWritePermission(getActivity(), new MyUtils.IntegerCallback() {
                    @Override
                    public void callback(int data) {
                        if (data>0){
                            mRequestCode = JUMP_TO_MAIN_EDIT;
                            selectFromAblum();
                        }
                    }
                });
                break;
            case R.id.btn_setting: //设置界面
                startActivity(new Intent(getActivity(),SettingActivity.class));
                break;
            case R.id.btn_video_remove_water://视频去水印
                Log.i("视频处理","视频去水印");
                MyUtils.checkReadAndWritePermission(getActivity(), new MyUtils.IntegerCallback() {
                    @Override
                    public void callback(int data) {
                        if (data>0){
                            mRequestCode = SELECT_VIDEO_REMOVE_WATER_MARK;
                            selectFromAblum();
                        }
                    }
                });
                break;
            case R.id.btn_video_add_watermark://视频加水印
                Log.i("视频处理","视频加水印");
                MyUtils.checkReadAndWritePermission(getActivity(), new MyUtils.IntegerCallback() {
                    @Override
                    public void callback(int data) {
                        if (data>0){
                            mRequestCode = SELECT_VIDEO_ADD_WATER_MARK;
                            selectFromAblum();
                        }
                    }
                });
                break;
            case R.id.btn_video_splice://视频拼接
                Log.i("视频处理","视频拼接");
                MyUtils.checkReadAndWritePermission(getActivity(), new MyUtils.IntegerCallback() {
                    @Override
                    public void callback(int data) {
                        if (data>0){
                            mRequestCode = SELECT_VIDEO_SPLICE;
                            selectFromAblum();
                        }
                    }
                });
                break;
            case R.id.btn_video_cut://视频剪裁
                Log.i("视频处理","视频剪裁");
                MyUtils.checkReadAndWritePermission(getActivity(), new MyUtils.IntegerCallback() {
                    @Override
                    public void callback(int data) {
                        if (data>0){
                            mRequestCode = SELECT_VIDEO_CROP;
                            selectFromAblum();
                        }
                    }
                });
                break;
            case R.id.btn_video_edit://视频分割
                Log.i("视频处理","视频分割");
                MyUtils.checkReadAndWritePermission(getActivity(), new MyUtils.IntegerCallback() {
                    @Override
                    public void callback(int data) {
                        if (data>0){
                            mRequestCode = SELECT_VIDEO_EDIT;
                            selectFromAblum();
                        }
                    }
                });
                break;
            case R.id.btn_video_music://背景音乐
                Log.i("视频处理","背景音乐");
                MyUtils.checkReadAndWritePermission(getActivity(), new MyUtils.IntegerCallback() {
                    @Override
                    public void callback(int data) {
                        if (data>0){
                            mRequestCode = SELECT_VIDEO_MUSIC;
                            selectFromAblum();
                        }
                    }
                });
                break;
            case R.id.btn_video_extract_bg://分离音频
                Log.i("视频处理","背景音乐");
                MyUtils.checkReadAndWritePermission(getActivity(), new MyUtils.IntegerCallback() {
                    @Override
                    public void callback(int data) {
                        if (data>0){
                            mRequestCode = SELECT_VIDEO_EXTRACT_BG;
                            selectFromAblum();
                        }
                    }
                });
                break;


            case R.id.btn_img_add_watermark:  //图片加水印
                MyUtils.checkReadAndWritePermission(getActivity(), new MyUtils.IntegerCallback() {
                    @Override
                    public void callback(int data) {
                        if (data>0){
                            mRequestCode = SELECT_IMAGE_ADD_WATER_MARK;
                            selectFromAblum();
                        }
                    }
                });
                break;
            case R.id.btn_img_remove_watermark:  //图片去水印
                MyUtils.checkReadAndWritePermission(getActivity(), new MyUtils.IntegerCallback() {
                    @Override
                    public void callback(int data) {
                        if (data>0){
                            mRequestCode = SELECT_IMAGE_REMOVE_WATER_MARK;
                            selectFromAblum();
                        }
                    }
                });
                break;
            case R.id.btn_img_palette:  //画板涂鸦
                MyUtils.checkReadAndWritePermission(getActivity(), new MyUtils.IntegerCallback() {
                    @Override
                    public void callback(int data) {
                        if (data>0){
                            mRequestCode = SELECT_IMAGE_FREE_EDIT;
                            selectFromAblum();
                        }
                    }
                });
                break;

            case R.id.btn_music_edit:  //音频分割
                MyUtils.checkReadAndWritePermission(getActivity(), new MyUtils.IntegerCallback() {
                    @Override
                    public void callback(int data) {
                        if (data>0){
                            mRequestCode = SELECT_MUSIC_CROP;
                            selectFromAblum();
                        }
                    }
                });
                break;
        }
    }

    /**
     * 从相册选择编辑图片
     */
    private void selectFromAblum() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            openAblumWithPermissionsCheck();
        } else {
            openAblum();
        }
    }

    private void openAblumWithPermissionsCheck() {
        if (ActivityCompat.checkSelfPermission(getActivity(),
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(),
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    REQUEST_PERMISSON_SORAGE);
            return;
        }
        openAblum();
    }

    private void openAblum() {
        switch (mRequestCode){
            case JUMP_TO_MAIN_EDIT://跳转到总编辑界面
                Log.i("视频处理","跳转到总编辑界面");
                PictureSelector.create(this).openGallery(PictureMimeType.ofVideo())
                        .previewVideo(true).selectionMode(PictureConfig.SINGLE).videoMinSecond(1).forResult(mRequestCode);
                break;
            case SELECT_VIDEO_ADD_WATER_MARK://视频加水印
                Log.i("视频处理","openAblum 视频加水印");
                PictureSelector.create(this).openGallery(PictureMimeType.ofVideo())
                        .previewVideo(true).selectionMode(PictureConfig.SINGLE).videoMinSecond(1).forResult(mRequestCode);
                break;
            case SELECT_VIDEO_REMOVE_WATER_MARK://视频去水印
                Log.i("视频处理","openAblum 视频去水印");
                PictureSelector.create(this).openGallery(PictureMimeType.ofVideo())
                        .previewVideo(true).selectionMode(PictureConfig.SINGLE).videoMinSecond(1).forResult(mRequestCode);
                break;
            case SELECT_VIDEO_EDIT://视频分割
                Log.i("视频处理","openAblum 视频分割");
                PictureSelector.create(this).openGallery(PictureMimeType.ofVideo())
                        .previewVideo(true).selectionMode(PictureConfig.SINGLE).videoMinSecond(1).forResult(mRequestCode);
                break;
            case SELECT_VIDEO_CROP://视频剪裁
                Log.i("视频处理","openAblum 视频剪裁");
                PictureSelector.create(this).openGallery(PictureMimeType.ofVideo())
                        .previewVideo(true).selectionMode(PictureConfig.SINGLE).videoMinSecond(1).forResult(mRequestCode);
                break;
            case SELECT_VIDEO_MUSIC://背景音乐
                Log.i("视频处理","openAblum 背景音乐");
                PictureSelector.create(this).openGallery(PictureMimeType.ofVideo())
                        .previewVideo(true).selectionMode(PictureConfig.SINGLE).videoMinSecond(1).forResult(mRequestCode);
                break;
            case SELECT_VIDEO_EXTRACT_BG://背景音乐
                Log.i("视频处理","openAblum 提取音频");
                PictureSelector.create(this).openGallery(PictureMimeType.ofVideo())
                        .previewVideo(true).selectionMode(PictureConfig.SINGLE).videoMinSecond(1).forResult(mRequestCode);
                break;
            case SELECT_VIDEO_SPLICE://视频拼接
                Log.i("视频处理","openAblum 视频拼接"); //最多5个
                PictureSelector.create(this).openGallery(PictureMimeType.ofVideo())
                        .previewVideo(true).maxSelectNum(5).selectionMode(PictureConfig.MULTIPLE).videoMinSecond(1).forResult(mRequestCode);
                break;
            case SELECT_MUSIC_CROP://音频分割
                Log.i("视频处理","openAblum 视频拼接"); //最多5个
                PictureSelector.create(this).openGallery(PictureMimeType.ofAudio())
                        .previewVideo(true).selectionMode(PictureConfig.SINGLE).videoMinSecond(1).forResult(mRequestCode);
                break;
            default:
                PictureSelector.create(this).openGallery(PictureMimeType.ofImage())
                        .previewVideo(true).selectionMode(PictureConfig.SINGLE).videoMinSecond(1).forResult(mRequestCode);
//                startActivityForResult(new Intent(getActivity(), SelectPictureActivity.class),
//                    mRequestCode);
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == REQUEST_PERMISSON_SORAGE
                && grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            openAblum();
            return;
        }
    }

    private String saveFilePath;
    private int imageWidth, imageHeight;
    private void handleEditorImage(Intent data) {
//        String newFilePath = data.getStringExtra(ImageEditorIntentBuilder.OUTPUT_PATH);
//        boolean isImageEdit = data.getBooleanExtra(EditImageActivity.IS_IMAGE_EDITED, false);

//        if (isImageEdit) { //编辑成功
//            //保存图片并进入我的作品
//            saveFilePath = outputFile.getAbsolutePath();
//            Toast.makeText(getActivity(), getString(R.string.save_path, saveFilePath), Toast.LENGTH_LONG).show();
//            //放入图片列表
//            DisplayMetrics metrics = getResources().getDisplayMetrics();
//            imageWidth = metrics.widthPixels;
//            imageHeight = metrics.heightPixels;
//
//            MyArt art = new MyArt();
//            art.setHeight(imageHeight);
//            art.setWidth(imageWidth);
//            Long millis = System.currentTimeMillis();
//            art.setCreate_time(millis + "");
//            art.setPath(saveFilePath);
//            art.setType(1); //加水印
//            art.save();
//
//            Intent intent = new Intent(getActivity(), ShowMyArtsActivity.class);
//            intent.putExtra(EXTRA_OUTPUT, saveFilePath);
//            intent.putExtra("width", imageHeight);
//            intent.putExtra("height", imageWidth);
//
//            FileUtil.ablumUpdate(getActivity(), saveFilePath);//更新相册
//            startActivity(intent);
//            finish();
//
//
//        } else {
////            newFilePath = data.getStringExtra(ImageEditorIntentBuilder.SOURCE_PATH);
//
//        }



    }

    private File outputFile;
    private List<LocalMedia> paths;
    public static final int ACTION_REQUEST_EDITIMAGE = 9;
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**
         * 选择图片后返回
         * **/
        if (resultCode == RESULT_OK) {
            String filepath = data.getStringExtra("imgPath");
            //原始图片存储地址
            switch (requestCode) {
                case REQUEST_CODE_SHOW_IMAGE://图片
                    //如果点击了删除
                    if (data!=null && data.getIntExtra("position",-1)!= -1){
                        int position = data.getIntExtra("position",-1);
                        //删除这条数据
                        deleteFromLitepal(position);
                    }
                    break;
                case REQUEST_CODE_SHOW_VIDEO://视频
                    //如果点击了删除
                    if (data!=null && data.getIntExtra("position",-1)!= -1){
                        int position = data.getIntExtra("position",-1);
                        //删除这条数据
                        deleteFromLitepal(position);

                    }
                    break;
                case REQUEST_CODE_SHOW_AUDIO://音频
                    //如果点击了删除
                    if (data!=null && data.getIntExtra("position",-1)!= -1){
                        int position = data.getIntExtra("position",-1);
                        //删除这条数据
                        deleteFromLitepal(position);
                    }
                    break;

                case SELECT_IMAGE_REMOVE_WATER_MARK://图片去水印

                    List<LocalMedia> selectlist01 = PictureSelector.obtainMultipleResult(data);
                    if(selectlist01.size()>0){
                        //验证成功，可以正确的去除选定区域的水印
                        startActivity(new Intent(getActivity(), RemoveWaterMark2Activity.class)
                                .putExtra("path",selectlist01.get(0).getPath())
                                .putExtra("img-width",selectlist01.get(0).getWidth())
                                .putExtra("img-height",selectlist01.get(0).getHeight())
                        );
                    }
                    break;
                case SELECT_IMAGE_ADD_WATER_MARK://图片加水印
                    List<LocalMedia> selectlist02 = PictureSelector.obtainMultipleResult(data);
                    if(selectlist02.size()>0){
                        //验证成功，可以正确的去除选定区域的水印
                        startActivity(new Intent(getActivity(), AddWaterMark02Activity.class)
                                .putExtra("path",selectlist02.get(0).getPath()));
                    }
                    break;
                case SELECT_IMAGE_FREE_EDIT://自由编辑
                    List<LocalMedia> selectlist03 = PictureSelector.obtainMultipleResult(data);
                    if(selectlist03.size()>0){
                        String inputPath = selectlist03.get(0).getPath(); //输入路径
                        outputFile = FileUtils.genEditFile();//输出路径
//                        try {
//                            Intent intent = new ImageEditorIntentBuilder(this, inputPath, outputFile.getAbsolutePath())
//                                    .withAddText()
//                                    .withPaintFeature()
//                                    .withFilterFeature()
//                                    .withRotateFeature()
//                                    .withCropFeature()
//                                    .withBrightnessFeature()
//                                    .withSaturationFeature()
//                                    .withBeautyFeature()
//                                    .forcePortrait(true)
//                                    .build();
//
//                            EditImageActivity.start(this, intent, ACTION_REQUEST_EDITIMAGE);
//                        } catch (Exception e) {
//                            Toast.makeText(this, R.string.iamutkarshtiwari_github_io_ananas_not_selected, Toast.LENGTH_SHORT).show();
//                            Log.e("Demo App", e.getMessage());
//                        }

                        startActivity(new Intent(getActivity(), Palette02Activity.class)
                                .putExtra("path",selectlist03.get(0).getPath()));
//                        startActivity(new Intent(getActivity(), Palette02Activity.class)
//                                .putExtra("path",selectlist03.get(0).getPath()));
                    }
                    break;
                case ACTION_REQUEST_EDITIMAGE: //自由编辑返回
                    handleEditorImage(data);
                    break;

                case SELECT_VIDEO_REMOVE_WATER_MARK: //视频去水印
                    Log.i("视频处理","onActivityResult 视频去水印");
                    List<LocalMedia> selectList2 = PictureSelector.obtainMultipleResult(data);
                    if(selectList2.size()>0){
                        //验证成功，可以正确的去除选定区域的水印
                        MyUtils.checkOrcopyFile(selectList2.get(0).getPath(), new MyUtils.StrCallback() {
                            @Override
                            public void callback(String path, int isNew) {
                                if (path!=null && path.length()>0){
                                    startActivity(new Intent(getActivity(), VideoRemoveWaterActivity.class)
                                            .putExtra("path",path));
                                }
                            }
                        });


                    }
                    break;
                case SELECT_VIDEO_ADD_WATER_MARK: //视频加水印
                    Log.i("视频处理","onActivityResult 视频加水印");
                    List<LocalMedia> selectList3 = PictureSelector.obtainMultipleResult(data);
                    if(selectList3.size()>0){
                        //验证成功，可以正确的去除选定区域的水印
                        MyUtils.checkOrcopyFile(selectList3.get(0).getPath(), new MyUtils.StrCallback() {
                            @Override
                            public void callback(String path, int isNew) {
                                if (path!=null && path.length()>0){
                                    startActivity(new Intent(getActivity(), VideoAddWater2Activity.class)
                                            .putExtra("path",path));
                                }
                            }
                        });


                    }
                    break;
                case SELECT_VIDEO_EDIT: //视频时长剪辑
                    Log.i("视频处理","onActivityResult 视频分割");
                    List<LocalMedia> selectList4 = PictureSelector.obtainMultipleResult(data);
                    if(selectList4.size()>0){
                        //验证成功，可以正确的去除选定区域的水印
                        String path = selectList4.get(0).getPath();
                        //视频分割界面
                        MyUtils.checkOrcopyFile(path, new MyUtils.StrCallback() {
                            @Override
                            public void callback(String path, int isNew) {
                                if (path!=null && path.length()>0){
                                    startActivity(new Intent(getActivity(), VideoTrimmerActivity.class)
                                            .putExtra(VideoTrimmerActivity.VIDEO_PATH_KEY, path));
                                }
                            }
                        });



//                        startActivity(new Intent(getActivity(), VideoEditActivity.class)
//                                .putExtra("path",selectList4.get(0).getPath()));
                    }
                    break;
                case SELECT_VIDEO_CROP: //视频区域剪裁
                    Log.i("视频处理","onActivityResult 视频剪裁");
                    List<LocalMedia> selectList5 = PictureSelector.obtainMultipleResult(data);
                    if(selectList5.size()>0){
                        //验证成功，可以正确的去除选定区域的水印
                        MyUtils.checkOrcopyFile(selectList5.get(0).getPath(), new MyUtils.StrCallback() {
                            @Override
                            public void callback(String path, int isNew) {
                                if (path!=null && path.length()>0){
                                    startActivity(new Intent(getActivity(), VideoCropActivity.class)
                                            .putExtra("path",path));
                                }
                            }
                        });


                    }
                    break;
                case SELECT_VIDEO_MUSIC: //背景音乐
                    Log.i("视频处理","onActivityResult 背景音乐");
                    List<LocalMedia> selectList6 = PictureSelector.obtainMultipleResult(data);
                    if(selectList6.size()>0){
                        //验证成功，可以正确的去除选定区域的水印
                        MyUtils.checkOrcopyFile(selectList6.get(0).getPath(), new MyUtils.StrCallback() {
                            @Override
                            public void callback(String path, int isNew) {
                                if (path!=null && path.length()>0){
                                    startActivity(new Intent(getActivity(), VideoMusicActivity.class)
                                            .putExtra("path",path));
                                }
                            }
                        });


                    }
                    break;
                case SELECT_VIDEO_EXTRACT_BG: //提取音频
                    Log.i("视频处理","onActivityResult 背景音乐");
                    List<LocalMedia> selectList9 = PictureSelector.obtainMultipleResult(data);
                    if(selectList9.size()>0){
                        String path = selectList9.get(0).getPath();
                        MyUtils.checkOrcopyFile(path, new MyUtils.StrCallback() {
                            @Override
                            public void callback(String path, int isNew) {
                                if (path!=null && path.length()>0){
                                    startActivity(new Intent(getActivity(), VideoTrimmerActivity.class)
                                            .putExtra(VideoTrimmerActivity.VIDEO_PATH_KEY, path)
                                            .putExtra("mode", "extract_bg")
                                    );
                                }
                            }
                        });
                    }
                    break;
                case SELECT_VIDEO_SPLICE: //视频拼接
                    Log.i("视频处理","onActivityResult 视频拼接");
                    List<LocalMedia> selectList7 = PictureSelector.obtainMultipleResult(data);
                    if(selectList7.size()>0){
                        List<VideoItem> videos = new ArrayList<>();
                        for (LocalMedia media:selectList7){
                            MyUtils.checkOrcopyFile(media.getPath(), new MyUtils.StrCallback() {
                                @Override
                                public void callback(String str, int isNew) {
                                    if (str!=null && str.length()>0){
                                        media.setPath(str);
                                    }
                                }
                            });
                        }
                        for (LocalMedia media:selectList7){
                            videos.add(VideoUtil.createVideoItem(media.getPath()));
                        }
                        ModelSingle.getInstance().setMode2(videos);
//                        startActivity(new Intent(getActivity(), VideoSpliceActivity.class));
                        startActivity(new Intent(getActivity(), VideoConcatActivity.class));
                    }
                    break;
                case SELECT_MUSIC_CROP: //音频裁剪
                    Log.i("视频处理","onActivityResult 音频分割");
                    List<LocalMedia> selectList8 = PictureSelector.obtainMultipleResult(data);
                    if(selectList8.size()>0){

                        //路径检测
                        MyUtils.checkOrcopyFile(selectList8.get(0).getPath(), new MyUtils.StrCallback() {
                            @Override
                            public void callback(String str, int isNew) {
                                if (str!=null && str.length()>0){
                                    //验证成功，可以正确的去除选定区域的水印
                                    Log.i("newFileName","newFileName======"+str);
                                    startActivity(new Intent(getActivity(), MusicCropActivity.class)
                                            .putExtra("path",str)
                                            .putExtra("isNew",isNew)
                                    );
                                }
                            }
                        });
                    }
                    break;
                case JUMP_TO_MAIN_EDIT: //视频总编辑界面
                    Log.i("视频处理","onActivityResult 视频总编辑界面");
                    List<LocalMedia> selectList10 = PictureSelector.obtainMultipleResult(data);
                    if(selectList10.size()>0){
                        //验证成功，可以正确的去除选定区域的水印
                        MyUtils.checkOrcopyFile(selectList10.get(0).getPath(), new MyUtils.StrCallback() {
                            @Override
                            public void callback(String path, int isNew) {
                                if (path!=null && path.length()>0){
                                    startActivity(new Intent(getActivity(), VideoMainEditActivity.class)
                                            .putExtra("path",path));

                                }
                            }
                        });

                    }
                    break;
            }
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        //刷新数据
        initData();
    }
}
